<template>
  <div class="detail-page">
    <van-nav-bar
      left-text="返回"
      @click-left="$router.back()"
      fixed
      title="面经详情"
    />
    <header class="header">
      <h1>{{obj.stem}}</h1>
      <p>
        {{obj.createdAt}} | {{obj.likeCount}} 浏览量 | {{obj.views}} 点赞数
      </p>
      <p>
        <img :src="obj.avatar" alt="" />
        <span>{{obj.creator}}</span>
      </p>
    </header>
    <main class="body">
        <p v-html="obj.content"></p>
    </main>
    <div class="opt">
      <van-icon  @click="add(obj.id)"  :class="{active:obj.likeFlag}" name="like-o"/>
      <van-icon  @click="xx(obj.id)"  :class="{active:obj.collectFlag}" name="star-o"/>
    </div>
  </div>
</template>

<script>
import { show, opt } from '@/api/Detail'
import { Toast } from 'vant'
// import { Toast } from 'vant'
export default {
  name: 'DetailPage',

  data () {
    return {
      obj: {}

    }
  },
  async created () {
    // console.log(this.$route.params.id)
    if (this.obj.likeFlag) {
      Toast('点赞成功')
    }
    const id = this.$route.params.id
    const res = await show({ id })
    // console.log(res.data.data)
    this.obj = res.data.data
  },
  methods: {
    async add (id) {
      await opt({
        id: id,
        optType: 1
      })
      // console.log(res)
      this.obj.likeFlag = !this.obj.likeFlag
      if (this.obj.likeFlag) {
        // console.log('点赞成功')
        Toast('点赞成功')
      } else {
        // console.log('取消')
        Toast('点赞失败')
      }

      // Toast(res.data.message)
    },
    async xx (id) {
      const xx = await opt({
        id: id,
        optType: 2
      })
      this.obj.collectFlag = !this.obj.collectFlag
      if (this.obj.collectFlag) {
        Toast('收藏成功')
      } else {
        Toast('收藏失败')
      }

      console.log(xx)

      // Toast(res.data.message)
    }
  }

}
</script>

<style lang="less" scoped>
.detail-page {
  margin-top: 44px;
  overflow: hidden;
  padding: 0 15px;
  .header {
    h1 {
      font-size: 24px;
    }
    p {
      color: #999;
      font-size: 12px;
      display: flex;
      align-items: center;
    }
    img {
      width: 40px;
      height: 40px;
      border-radius: 50%;
      overflow: hidden;
    }
  }
  .opt {
    position: fixed;
    bottom: 100px;
    right: 0;
    > .van-icon {
      margin-right: 20px;
      background: #fff;
      width: 40px;
      height: 40px;
      line-height: 40px;
      text-align: center;
      border-radius: 50%;
      box-shadow: 2px 2px 10px #ccc;
      font-size: 18px;
      &.active {
        background: #FEC635;
        color: #fff;
      }
    }
  }
}
</style>
